package com.sangmo.fts.process.inventory.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.common.session.UserSession;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.PageSearch;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.plugin.dataview.crud.service.DelegateViewService;
import com.sangmo.boot.framework.plugin.dataview.crud.service.ViewService;
import com.sangmo.fts.process.inventory.model.IfContract;
import com.sangmo.fts.process.inventory.model.IfLadingBill;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class IfContractService extends DelegateViewService<IfContract> {

    @Autowired
    private IfContractEntityService ifContractEntityService;

    @Override
    protected ViewService<IfContract> getEntityService() {
        return ifContractEntityService;
    }


    public void pledgorConfirm(Long id) {
        IfContract contract = ifContractEntityService.get(id);
        if (null == contract.getPledgorDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ifContract.pledgorConfirm.permit.disabled");
        }
        if (contract.isPledgorConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ifContract.pledgorConfirm.state.wrong");
        }
        ifContractEntityService.pledgorConfirm(contract);
    }


    public void pledgeeConfirm(Long id) {
        IfContract contract = ifContractEntityService.get(id);
        if (null == contract.getPledgeeDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ifContract.pledgeeConfirm.permit.disabled");
        }
        if (contract.isPledgeeConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ifContract.pledgeeConfirm.state.wrong");
        }
        ifContractEntityService.pledgeeConfirm(contract);
    }

    public void regulatorConfirm(Long id) {
        IfContract contract = ifContractEntityService.get(id);
        if (null == contract.getRegulatorDoc()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ifContract.regulatorConfirm.permit.disabled");
        }
        if (contract.isRegulatorConfirm()) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ifContract.regulatorConfirm.state.wrong");
        }
        ifContractEntityService.regulatorConfirm(contract);
    }

    public PageResult<IfContract> selection(PageSearch ps) {
        ps.getFilters().add(new ValueFilter("pledgeeConfirm", ValueFilter.OP_EQ, true));
        ps.getFilters().add(new ValueFilter("pledgorConfirm", ValueFilter.OP_EQ, true));
        ps.getFilters().add(new ValueFilter("regulatorConfirm", ValueFilter.OP_EQ, true));
        return ifContractEntityService.search(ps);
    }

}
